# coding=utf-8
'''
Created on Oct 31, 2009

@author: victor
'''
from xml.dom import minidom


def setXmlType(response):
    response.headers.__delitem__('Content-Type')
    response.headers.add_header('Content-Type', 'text/xml; charset=utf-8')


def setXhtmlType(response):
    response.headers.__delitem__('Content-Type')
    response.headers.add_header('Content-Type', 'application/xhtml+xml; charset=utf-8')


class HtmlDom(minidom.Document):
    '''
    classdocs
    '''


    def __init__(self, titleText='possum', cssName=None):
        '''
        Constructor
        '''
        self.html = self.createElement('html')

        head = self.setNode(self.html, 'head')
        title = self.setNode(head, 'title')
        self.setText(title, titleText)

        link = self.setNode(head, 'link')
        link.setAttribute('href', '/css/index.css')
        link.setAttribute('rel', 'stylesheet')
        link.setAttribute('type', 'text/css')

        if cssName:
            link = self.setNode(head, 'link')
            link.setAttribute('href', '/css/' + cssName)
            link.setAttribute('rel', 'stylesheet')
            link.setAttribute('type', 'text/css')

        body = self.setNode(self.html, 'body')

        self.header = self.setNode(body, 'div')
        self.setClass(self.header, 'header')

        self.center_main = self.setNode(body, 'div')
        self.setClass(self.center_main, 'center_main')

        self.center = self.setNode(body, 'div')
        self.setClass(self.center, 'center')
        self.left = self.setNode(body, 'div')
        self.setClass(self.left, 'left')
        self.right = self.setNode(body, 'div')
        self.setClass(self.right, 'right')

        self.footer = self.setNode(body, 'div')
        self.setClass(self.footer, 'footer')





    def setLink(self, parent, name, destination):
        node = self.setNode(parent, 'a')
        node.setAttribute('href', destination)
        self.setText(node, name)

    def setNode(self, parent, name):
        node = self.createElement(name)
        parent.appendChild(node)
        return node

    def setText(self, parent, text):
        textNode = self.createTextNode(text)
        parent.appendChild(textNode)
        return textNode

    def setClass(self, tag, value):
        tag.setAttribute('class', value)

    def getHtml(self):
        #return self.html.toxml()
        return r"""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">""" + self.html.toxml()


